package com.yn.club.auth.domain.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yn.club.auth.domain.entity.AuthPermissionDO;
import com.yn.club.auth.infra.basic.entity.AuthPermission;
import com.yn.club.common.enums.IsDeletedFlagEnum;
import com.yn.club.common.util.PageReq;
import com.yn.club.common.util.PageResp;
import java.io.Serializable;
import java.time.LocalDateTime;
import java.util.List;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import com.yn.club.auth.domain.service.AuthPermissionDomainService;
import com.yn.club.auth.infra.basic.service.AuthPermissionService;
import com.yn.club.auth.domain.convert.AuthPermissionDomainConverter;


/**
 * (AuthPermission)表服务接口实现类
 *
 * @author YN
 * @since 2025-01-22 10:07:43
 */
@Service
@RequiredArgsConstructor
@Slf4j
public class AuthPermissionDomainServiceImpl implements AuthPermissionDomainService {

  private final AuthPermissionService authPermissionService;
  private final AuthPermissionDomainConverter authPermissionDomainConverter;


  /**
   * 分页
   */
  @Override
  public PageResp<AuthPermissionDO> page(PageReq pageReq, AuthPermissionDO query){
    QueryWrapper<AuthPermission> querywrapper = new QueryWrapper<AuthPermission>()
        .eq(query.getId() != null, "id", query.getId())
        .orderByDesc("id");
        
    Page<AuthPermission> page = authPermissionService.page(
      new Page<>(pageReq.getPageIndex(),pageReq.getPageSize()),querywrapper);
    List<AuthPermission> list = page.getRecords();  
    return PageResp.of(page,authPermissionDomainConverter.convertEntityToDo(list));
  }

  /**
   * 新增数据
   */
  @Override
  public Boolean insert(AuthPermissionDO obj){
    AuthPermission authPermission = authPermissionDomainConverter.convertDoToEntity(obj);
    authPermission.setIsDeleted(IsDeletedFlagEnum.UN_DELETED.getCode());
    authPermission.setCreatedTime(LocalDateTime.now());
    authPermission.setUpdateTime(LocalDateTime.now());
    return authPermissionService.save(authPermission);
  };


  /**
   * 更新数据
   */
  @Override
  public Boolean updateById(AuthPermissionDO obj){
    AuthPermission authPermission = authPermissionDomainConverter.convertDoToEntity(obj);
    authPermission.setUpdateTime(LocalDateTime.now());
    return authPermissionService.updateById(authPermission);
  };

  /**
   * 批量删除
   */
  @Override
  public Boolean delete(List<Integer> ids){
    UpdateWrapper<AuthPermission> set = new UpdateWrapper<AuthPermission>()
        .in("id", ids)
        .set("is_deleted", IsDeletedFlagEnum.DELETED.getCode());
    return authPermissionService.update(set);
  };

  /**
   * 根据id查找
   */
  @Override
  public AuthPermissionDO getById(Serializable id){
    AuthPermission authPermission = authPermissionService.getById(id);
    return  authPermissionDomainConverter.convertEntityToDo(authPermission);
  }

  @Override
  public Boolean getPermissionByName(String userName) {
    return null;
  };


}

